/**create by liuhua at 2020年10月22日 下午4:36:12**/
package com.star.aries.controller;

import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.star.aries.constant.ErrorCodeEnum;
import com.star.aries.dto.BaseResult;
import com.star.aries.dto.req.TrainTypeSaveRequestDto;
import com.star.aries.dto.req.TrainTypeUpdateRequestDto;
import com.star.aries.dto.res.TrainTypeListResponseDto;
import com.star.aries.exception.HnServiceException;
import com.star.aries.service.TrainTypeService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Validated
@Tag(name = "车型")
@RestController
@RequestMapping("/train/type")
public class TrainTypeController {
  @Autowired
  private TrainTypeService trainTypeService;
  
  @PostMapping(value = "/list")
  @Operation(summary = "车型列表", method = "POST")
  public BaseResult<List<TrainTypeListResponseDto>> listTrainType() {
    try {
      List<TrainTypeListResponseDto> list = trainTypeService.listTrainType();
      return BaseResult.success(list);
    } catch (HnServiceException e) {
      return BaseResult.fail(e.getCode(), e.getMsg());
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return BaseResult.fail(ErrorCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
  }
  
  @PostMapping(value = "/add")
  @Operation(summary = "添加车型", method = "POST")
  public BaseResult<String> addTrainType(@Valid @RequestBody TrainTypeSaveRequestDto saveDto) {
    try {
      String res = trainTypeService.addTrainType(saveDto);
      return BaseResult.success(res);
    } catch (HnServiceException e) {
      return BaseResult.fail(e.getCode(), e.getMsg());
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return BaseResult.fail(ErrorCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
  }
  
  @PostMapping(value = "/update")
  @Operation(summary = "编辑车型", method = "POST")
  public BaseResult<Void> updateTrainType(@Valid @RequestBody TrainTypeUpdateRequestDto updateDto) {
    try {
      trainTypeService.updateTrainType(updateDto);
      return BaseResult.success();
    } catch (HnServiceException e) {
      return BaseResult.fail(e.getCode(), e.getMsg());
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return BaseResult.fail(ErrorCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
  }
  
  @PostMapping(value = "/delete")
  @Operation(summary = "删除车型", method = "POST")
  public BaseResult<Void> deleteTrainType(@Parameter(description = "车型id") @RequestParam(required = false) @NotNull(message = "trainTypeId 必填") Long trainTypeId) {
    try {
      trainTypeService.deleteTrainType(trainTypeId);
      return BaseResult.success();
    } catch (HnServiceException e) {
      return BaseResult.fail(e.getCode(), e.getMsg());
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return BaseResult.fail(ErrorCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
  }
  
  @PostMapping(value = "/open/folder")
  @Operation(summary = "打开文件夹", method = "POST")
  public BaseResult<Void> openFolder(@Parameter(description = "车型id") @RequestParam(required = false) @NotNull(message = "trainTypeId 必填") Long trainTypeId) {
    try {
      trainTypeService.openFolder(trainTypeId);
      return BaseResult.success();
    } catch (HnServiceException e) {
      return BaseResult.fail(e.getCode(), e.getMsg());
    } catch (Throwable e) {
      log.error(e.getMessage(), e);
      return BaseResult.fail(ErrorCodeEnum.SYSTEM_ERROR.getCode(), e.getMessage());
    }
  }
}
